package com.citi.bond.controller;

import com.citi.bond.domain.User;
import com.citi.bond.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.util.DigestUtils;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/login"})
    public String login(){return "pages/login";}

    @RequestMapping(value = {"/passwordLogin"})
    public String passwordLogin(Model model, @RequestParam("userId") Integer id, @RequestParam("password") String password, HttpServletRequest request){
        User user = userService.findUserById(id);

//        String md5Pass = DigestUtils.md5DigestAsHex("123456".getBytes());
//        user.setPass(md5Pass);

        if((user!= null) && (user.getPass().equals(password))){ //登录成功
            userService.setUserInfo(user,model);
            HttpSession session = request.getSession();
            session.setAttribute("id",id);
            return "pages/bondIndex";
        }
        return "pages/loginError";
    }


}
